<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册 - {{ app_name }}</title>
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/css/fontawesome.min.css" rel="stylesheet">
    <link href="/static/css/style.css" rel="stylesheet">
    <link rel="icon" href="/static/favicon.ico" type="image/x-icon">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px 0;
        }
        .register-container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            width: 100%;
            max-width: 500px;
        }
        .register-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }
        .register-body {
            padding: 40px 30px;
        }
        .form-floating {
            margin-bottom: 20px;
        }
        .btn-register {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            padding: 12px;
            font-weight: 600;
            width: 100%;
        }
        .btn-register:hover {
            background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
        }
        .register-links {
            text-align: center;
            margin-top: 20px;
        }
        .register-links a {
            color: #667eea;
            text-decoration: none;
        }
        .register-links a:hover {
            text-decoration: underline;
        }
        .password-strength {
            margin-top: 10px;
        }
        .strength-bar {
            height: 4px;
            border-radius: 2px;
            transition: all 0.3s;
        }
        .strength-text {
            font-size: 12px;
            margin-top: 5px;
        }
    </style>
</head>
<body>
    <div class="register-container">
        <div class="register-header">
            <h3><i class="fas fa-user-plus me-2"></i>用户注册</h3>
            <p class="mb-0">创建您的账户</p>
        </div>
        
        <div class="register-body">
            {% if error %}
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <i class="fas fa-exclamation-circle me-2"></i>{{ error }}
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
            {% endif %}
            
            {% if success %}
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <i class="fas fa-check-circle me-2"></i>{{ success }}
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
            {% endif %}
            
            <form id="registerForm" method="POST" action="/register">
                <div class="form-floating">
                    <input type="text" class="form-control" id="username" name="username" placeholder="用户名" required>
                    <label for="username"><i class="fas fa-user me-2"></i>用户名</label>
                </div>
                
                <div class="form-floating">
                    <input type="email" class="form-control" id="email" name="email" placeholder="邮箱" required>
                    <label for="email"><i class="fas fa-envelope me-2"></i>邮箱</label>
                </div>

                <div class="form-floating">
                    <div class="input-group">
                        <input type="text" class="form-control" id="verification_code" name="verification_code" placeholder="验证码" required>
                        <button type="button" class="btn btn-outline-secondary" id="sendCodeBtn">发送验证码</button>
                    </div>
                    <label for="verification_code" class="mt-2"><i class="fas fa-shield-alt me-2"></i>验证码</label>
                </div>
                
                <div class="form-floating">
                    <input type="tel" class="form-control" id="phone" name="phone" placeholder="手机号">
                    <label for="phone"><i class="fas fa-phone me-2"></i>手机号（可选）</label>
                </div>
                
                <div class="form-floating">
                    <input type="password" class="form-control" id="password" name="password" placeholder="密码" required>
                    <label for="password"><i class="fas fa-lock me-2"></i>密码</label>
                </div>
                
                <div class="password-strength">
                    <div class="progress">
                        <div class="progress-bar strength-bar" role="progressbar" style="width: 0%"></div>
                    </div>
                    <div class="strength-text text-muted">密码强度：弱</div>
                </div>
                
                <div class="form-floating">
                    <input type="password" class="form-control" id="confirm_password" name="confirm_password" placeholder="确认密码" required>
                    <label for="confirm_password"><i class="fas fa-lock me-2"></i>确认密码</label>
                </div>
                
                <div class="form-check mb-3">
                    <input class="form-check-input" type="checkbox" id="agree" name="agree" required>
                    <label class="form-check-label" for="agree">
                        我同意 <a href="/service-agreement" target="_blank">服务条款</a> 和 <a href="/privacy-policy" target="_blank">隐私政策</a>
                    </label>
                </div>
                
                <button type="submit" class="btn btn-primary btn-register">
                    <i class="fas fa-user-plus me-2"></i>注册账户
                </button>
            </form>
            
            <div class="register-links">
                <p>已有账户？ <a href="/login"><i class="fas fa-sign-in-alt me-1"></i>立即登录</a></p>
            </div>
        </div>
    </div>

    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/js/bootstrap.bundle.min.js"></script>
    <script>
        $(document).ready(function() {
            // 发送验证码
            let countdown = 60;
            let timer = null;
            let isEmailSent = false;

            $('#sendCodeBtn').on('click', function() {
                const email = $('#email').val().trim();
                const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

                if (!emailRegex.test(email)) {
                    alert('请输入有效的邮箱地址');
                    return;
                }

                if (timer !== null) {
                    return;
                }

                // 发送验证码请求
                $.ajax({
                    url: '/send-verification-code',
                    method: 'POST',
                    data: { email: email },
                    success: function(response) {
                        alert('验证码已发送到您的邮箱，请查收');
                        isEmailSent = true;

                        // 开始倒计时
                        countdown = 60;
                        $(this).prop('disabled', true);
                        $(this).text(`重新发送(${countdown}s)`);

                        timer = setInterval(() => {
                            countdown--;
                            if (countdown <= 0) {
                                clearInterval(timer);
                                timer = null;
                                $(this).prop('disabled', false);
                                $(this).text('发送验证码');
                            } else {
                                $(this).text(`重新发送(${countdown}s)`);
                            }
                        }, 1000);
                    }.bind(this),
                    error: function(xhr) {
                        const error = xhr.responseJSON?.detail || '发送失败，请重试';
                        alert(error);
                    }
                });
            });

            // 密码强度检测
            // 密码强度检测
            $('#password').on('input', function() {
                const password = $(this).val();
                const strength = checkPasswordStrength(password);
                updatePasswordStrength(strength);
            });
            
            // 表单验证
            $('#registerForm').on('submit', function(e) {
                const username = $('#username').val().trim();
                const email = $('#email').val().trim();
                const password = $('#password').val();
                const confirmPassword = $('#confirm_password').val();
                const agree = $('#agree').is(':checked');
                
                // 用户名验证
                if (username.length < 3) {
                    e.preventDefault();
                    alert('用户名至少需要3个字符');
                    return false;
                }
                
                // 邮箱验证
                const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
                if (!emailRegex.test(email)) {
                    e.preventDefault();
                    alert('请输入有效的邮箱地址');
                    return false;
                }

                // 验证码验证
                const verificationCode = $('#verification_code').val().trim();
                if (!isEmailSent) {
                    e.preventDefault();
                    alert('请先发送验证码');
                    return false;
                }
                if (!verificationCode) {
                    e.preventDefault();
                    alert('请输入验证码');
                    return false;
                }
                
                // 密码验证
                if (password.length < 6) {
                    e.preventDefault();
                    alert('密码至少需要6个字符');
                    return false;
                }
                
                if (password !== confirmPassword) {
                    e.preventDefault();
                    alert('两次输入的密码不一致');
                    return false;
                }
                
                if (!agree) {
                    e.preventDefault();
                    alert('请同意服务条款和隐私政策');
                    return false;
                }
            });
        });
        
        function checkPasswordStrength(password) {
            let score = 0;
            let feedback = [];
            
            // 长度检查
            if (password.length >= 8) score += 25;
            else if (password.length >= 6) score += 15;
            else score += 5;
            
            // 字符类型检查
            if (/[a-z]/.test(password)) score += 15;
            if (/[A-Z]/.test(password)) score += 15;
            if (/[0-9]/.test(password)) score += 15;
            if (/[^a-zA-Z0-9]/.test(password)) score += 30;
            
            // 连续字符检查
            if (!/(.)\1{2,}/.test(password)) score += 10;
            
            // 返回结果
            if (score >= 80) return { level: 'strong', color: 'success', text: '非常强' };
            else if (score >= 60) return { level: 'good', color: 'info', text: '强' };
            else if (score >= 40) return { level: 'medium', color: 'warning', text: '中等' };
            else return { level: 'weak', color: 'danger', text: '弱' };
        }
        
        function updatePasswordStrength(strength) {
            $('.strength-bar')
                .css('width', strength.score + '%')
                .removeClass('bg-success bg-info bg-warning bg-danger')
                .addClass('bg-' + strength.color);
            
            $('.strength-text')
                .removeClass('text-success text-info text-warning text-danger')
                .addClass('text-' + strength.color)
                .text('密码强度：' + strength.text);
        }
    </script>
</body>
</html>